package com.elong.android.specialhouse.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.JSONConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.entity.ApartmentKeyword;
import com.elong.android.specialhouse.entity.ApartmentSearchChildDataInfo;
import com.elong.android.specialhouse.entity.ApartmentSearchDataInfo;
import com.elong.android.specialhouse.entity.ApartmentSelectSerchChildInfo;
import com.elong.infrastructure.entity.Group;

public class ApartmentAreaUtils {

	/**
	 * 将API返回的区域原始JSON数据(getLocationListV2接口)
	 * result转换成目前代码中需要的ApartmentSearchDataInfo,包含商圈,行政区,地铁,机场/火车站
	 * 
	 * @param result
	 * @return
	 */
	public static ApartmentSearchDataInfo createHotelAreaData(Object result) {
		ApartmentSearchDataInfo districtSerchDataInfo = null;
		try {
			if (result != null) {
				districtSerchDataInfo = new ApartmentSearchDataInfo();
				districtSerchDataInfo.setName("区域位置");
				districtSerchDataInfo.setSelect(true);
				districtSerchDataInfo.setIconResource(R.drawable.icon_district_selecter);
				Group<ApartmentSearchChildDataInfo> districtchildDataInfos = new Group<ApartmentSearchChildDataInfo>();
				districtSerchDataInfo.setChildDataInfos(districtchildDataInfos);

				ApartmentSearchChildDataInfo standhHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> standChild = null;
				ApartmentSearchChildDataInfo businessHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> businessChild = null;
				ApartmentSearchChildDataInfo regionHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> regionChild = null;
				ApartmentSearchChildDataInfo hospitalHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> hospitalChild = null;
				ApartmentSearchChildDataInfo schoolHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> schoolChild = null;					

				// 商圈
				businessHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(R.drawable.icon_hotel_district, "商圈",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				businessChild = new Group<ApartmentSearchChildDataInfo>();
				businessHotelSerchChildDataInfo.setChildDataInfos(businessChild);
				districtchildDataInfos.add(businessHotelSerchChildDataInfo);
				// 行政区
				regionHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_administrative_district, "行政区", false, districtSerchDataInfo.getName(),
						districtSerchDataInfo);
				regionChild = new Group<ApartmentSearchChildDataInfo>();
				regionHotelSerchChildDataInfo.setChildDataInfos(regionChild);
				districtchildDataInfos.add(regionHotelSerchChildDataInfo);
				// 地铁
				ApartmentSearchChildDataInfo metroHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_train, "地铁", false, districtSerchDataInfo.getName(),
						districtSerchDataInfo);
				districtchildDataInfos.add(metroHotelSerchChildDataInfo);
				// 机场/火车站
				standhHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_plane, "机场/火车站",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				standChild = new Group<ApartmentSearchChildDataInfo>();
				standhHotelSerchChildDataInfo.setChildDataInfos(standChild);
				districtchildDataInfos.add(standhHotelSerchChildDataInfo);
				
				//医院
				hospitalHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_plane, "医院",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				hospitalChild = new Group<ApartmentSearchChildDataInfo>();
				hospitalHotelSerchChildDataInfo.setChildDataInfos(hospitalChild);
				districtchildDataInfos.add(hospitalHotelSerchChildDataInfo);
				
				//学校
				schoolHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_plane, "学校",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				schoolChild = new Group<ApartmentSearchChildDataInfo>();
				schoolHotelSerchChildDataInfo.setChildDataInfos(schoolChild);
				districtchildDataInfos.add(schoolHotelSerchChildDataInfo);
				// 品牌
				// ApartmentSearchChildDataInfo brandHotelSerchChildDataInfo = new
				// ApartmentSearchChildDataInfo(R.drawable.icon_brand_selecter,
				// "品牌", false,
				// districtSerchDataInfo.getName(), districtSerchDataInfo);
				// Group<ApartmentSearchChildDataInfo> brandChild = new
				// Group<ApartmentSearchChildDataInfo>();
				// brandHotelSerchChildDataInfo.setChildDataInfos(brandChild);
				// districtchildDataInfos.add(brandHotelSerchChildDataInfo);

				// 解析地铁数据
//				JSONArray subwayTagIDs = ((JSONObject) result).getJSONArray(JSONConstants.ATTR_SUBWAYSTATIONTAGINFOS);
//				Group<ApartmentSearchChildDataInfo> metroHotelSerchChildDataInfos = new Group<ApartmentSearchChildDataInfo>();
//				metroHotelSerchChildDataInfo.setChildDataInfos(metroHotelSerchChildDataInfos);
//				int size = subwayTagIDs.size();
//				for (int i = 0; i < size; i++) {
//					JSONObject line = (JSONObject) subwayTagIDs.get(i);
//					ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo(0,
//							line.getString(JSONConstants.ATTR_TAGNAME), false, districtSerchDataInfo.getName(),
//							metroHotelSerchChildDataInfo);
//					metroHotelSerchChildDataInfos.add(dataInfo);
//					processSubwayLineInfo((JSONObject) result, dataInfo.getName(), dataInfo);
//				}

				JSONObject obj = (JSONObject) result;
				JSONArray array = obj.getJSONArray(JSONConstants.ATTR_LOCATIONLIST);
				int len = array.size();
				for (int i = 0; i < len; i++) {
					JSONObject list = (JSONObject) array.get(i);
					String typeName = list.getString(JSONConstants.ATTR_TYPEID);
					if (TextUtils.isEmpty(typeName)) {
						continue;
					}
					ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo();
					// if (typeName.equals(JSONConstants.ATTR_HOTEL_BRAND) &&
					// brandHotelSerchChildDataInfo != null) {
					// dataInfo.setParentInfo(brandHotelSerchChildDataInfo);
					// dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
					// dataInfo.setLeaf(true);
					// dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
					// dataInfo.setParentName(districtSerchDataInfo.getName());
					// brandChild.add(dataInfo);
					// } else
					if (typeName.equals(JSONConstants.ATTR_TRAFFIC) && standhHotelSerchChildDataInfo != null) {
						dataInfo.setParentInfo(standhHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						standChild.add(dataInfo);
					} else if (typeName.equals(JSONConstants.ATTR_COMMERCIAL)
							&& businessHotelSerchChildDataInfo != null) {
						dataInfo.setParentInfo(businessHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						businessChild.add(dataInfo);
					} else if (typeName.equals(JSONConstants.ATTR_DISTRICT) && regionHotelSerchChildDataInfo != null) {
						dataInfo.setParentInfo(regionHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						regionChild.add(dataInfo);
					} else if(typeName.equals(JSONConstants.ATTR_HOSPITAL) && hospitalHotelSerchChildDataInfo != null){
						dataInfo.setParentInfo(hospitalHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						hospitalChild.add(dataInfo);
					}else if(typeName.equals(JSONConstants.ATTR_SCHOOL) && schoolHotelSerchChildDataInfo != null){
						dataInfo.setParentInfo(schoolHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						schoolChild.add(dataInfo);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return districtSerchDataInfo;
	}

	/**
	 * 将API返回的区域原始JSON数据(getLocationListV2接口)
	 * result转换成目前代码中需要的ApartmentSearchDataInfo,包含商圈,行政区,地铁,机场/火车站,品牌
	 * 
	 * @param result
	 * @return
	 */
	public static ApartmentSearchDataInfo createApartmentAreaDataWithBrand(Object result) {
		ApartmentSearchDataInfo districtSerchDataInfo = null;
		try {
			if (result != null) {
				districtSerchDataInfo = new ApartmentSearchDataInfo();
				districtSerchDataInfo.setName("区域位置");
				districtSerchDataInfo.setSelect(true);
				districtSerchDataInfo.setIconResource(R.drawable.icon_district_selecter);
				Group<ApartmentSearchChildDataInfo> districtchildDataInfos = new Group<ApartmentSearchChildDataInfo>();
				districtSerchDataInfo.setChildDataInfos(districtchildDataInfos);

				ApartmentSearchChildDataInfo standhHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> standChild = null;
				ApartmentSearchChildDataInfo businessHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> businessChild = null;
				ApartmentSearchChildDataInfo regionHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> regionChild = null;
				ApartmentSearchChildDataInfo hospitalHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> hospitalChild = null;	
				ApartmentSearchChildDataInfo schoolHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> schoolChild = null;		

				// 商圈
				businessHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(R.drawable.icon_hotel_district, "商圈",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				businessChild = new Group<ApartmentSearchChildDataInfo>();
				businessHotelSerchChildDataInfo.setChildDataInfos(businessChild);
				districtchildDataInfos.add(businessHotelSerchChildDataInfo);
				// 行政区
				regionHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_administrative_district, "行政区", false, districtSerchDataInfo.getName(),
						districtSerchDataInfo);
				regionChild = new Group<ApartmentSearchChildDataInfo>();
				regionHotelSerchChildDataInfo.setChildDataInfos(regionChild);
				districtchildDataInfos.add(regionHotelSerchChildDataInfo);
				// 地铁
				ApartmentSearchChildDataInfo metroHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
						R.drawable.icon_hotel_train, "地铁", false, districtSerchDataInfo.getName(),
						districtSerchDataInfo);
				districtchildDataInfos.add(metroHotelSerchChildDataInfo);
				// 机场/火车站
				standhHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(R.drawable.icon_hotel_plane, "机场/火车站",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				standChild = new Group<ApartmentSearchChildDataInfo>();
				standhHotelSerchChildDataInfo.setChildDataInfos(standChild);
				districtchildDataInfos.add(standhHotelSerchChildDataInfo);
				
				//医院
				hospitalHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(R.drawable.icon_hotel_plane, "医院",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				hospitalChild = new Group<ApartmentSearchChildDataInfo>();
				hospitalHotelSerchChildDataInfo.setChildDataInfos(hospitalChild);
				districtchildDataInfos.add(hospitalHotelSerchChildDataInfo);
				
				//学校
				schoolHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(R.drawable.icon_hotel_plane, "学校",
						false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				schoolChild = new Group<ApartmentSearchChildDataInfo>();
				schoolHotelSerchChildDataInfo.setChildDataInfos(schoolChild);
				districtchildDataInfos.add(schoolHotelSerchChildDataInfo);
				// 品牌
//				ApartmentSearchChildDataInfo brandHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
//						R.drawable.icon_brand_selecter, "酒店品牌", false, districtSerchDataInfo.getName(),
//						districtSerchDataInfo);
//				Group<ApartmentSearchChildDataInfo> brandChild = new Group<ApartmentSearchChildDataInfo>();
//				brandHotelSerchChildDataInfo.setChildDataInfos(brandChild);
//				districtchildDataInfos.add(brandHotelSerchChildDataInfo);

				// 解析地铁数据
//				JSONArray subwayTagIDs = ((JSONObject) result).getJSONArray(JSONConstants.ATTR_SUBWAYSTATIONTAGINFOS);
//				Group<ApartmentSearchChildDataInfo> metroHotelSerchChildDataInfos = new Group<ApartmentSearchChildDataInfo>();
//				metroHotelSerchChildDataInfo.setChildDataInfos(metroHotelSerchChildDataInfos);
//				int size = subwayTagIDs.size();
//				for (int i = 0; i < size; i++) {
//					JSONObject line = (JSONObject) subwayTagIDs.get(i);
//					ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo(0,
//							line.getString(JSONConstants.ATTR_TAGNAME), false, districtSerchDataInfo.getName(),
//							metroHotelSerchChildDataInfo);
//					metroHotelSerchChildDataInfos.add(dataInfo);
//					processSubwayLineInfo((JSONObject) result, dataInfo.getName(), dataInfo);
//				}

				JSONObject obj = (JSONObject) result;
				JSONArray array = obj.getJSONArray(JSONConstants.ATTR_LOCATIONLIST);
				int len = array.size();
				for (int i = 0; i < len; i++) {
					JSONObject list = (JSONObject) array.get(i);
					String typeName = list.getString(JSONConstants.ATTR_TYPEID);
					if (TextUtils.isEmpty(typeName)) {
						continue;
					}
					ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo();
//					if (typeName.equals(JSONConstants.ATTR_HOTEL_BRAND) && brandHotelSerchChildDataInfo != null) {
//						dataInfo.setParentInfo(brandHotelSerchChildDataInfo);
//						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
//						dataInfo.setLeaf(true);
//						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
//						dataInfo.setParentName(districtSerchDataInfo.getName());
//						brandChild.add(dataInfo);
//					} else 
					if (typeName.equals(JSONConstants.ATTR_TRAFFIC) && standhHotelSerchChildDataInfo != null) {
						dataInfo.setParentInfo(standhHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						standChild.add(dataInfo);
					} else if (typeName.equals(JSONConstants.ATTR_COMMERCIAL)
							&& businessHotelSerchChildDataInfo != null) {
						dataInfo.setParentInfo(businessHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						businessChild.add(dataInfo);
					} else if (typeName.equals(JSONConstants.ATTR_DISTRICT) && regionHotelSerchChildDataInfo != null) {
						dataInfo.setParentInfo(regionHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						regionChild.add(dataInfo);
					}else if(typeName.equals(JSONConstants.ATTR_HOSPITAL) && hospitalHotelSerchChildDataInfo != null){
						dataInfo.setParentInfo(hospitalHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						hospitalChild.add(dataInfo);						
					}else if(typeName.equals(JSONConstants.ATTR_SCHOOL) && schoolHotelSerchChildDataInfo != null){
						dataInfo.setParentInfo(schoolHotelSerchChildDataInfo);
						dataInfo.setLeaf(true);
						dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
						dataInfo.setParentName(districtSerchDataInfo.getName());
						dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
						dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
						schoolChild.add(dataInfo);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return districtSerchDataInfo;
	}

	/**
	 * 处理地铁站数据
	 * 
	 * @param result
	 * @param line
	 * @param dataInfo
	 * @throws JSONException
	 */
	public static void processSubwayLineInfo(JSONObject result, String line, ApartmentSearchChildDataInfo dataInfo)
			throws JSONException {
		JSONArray array = result.getJSONArray(JSONConstants.ATTR_LOCATIONLIST);
		Group<ApartmentSearchChildDataInfo> childDataInfos = new Group<ApartmentSearchChildDataInfo>();
		dataInfo.setChildDataInfos(childDataInfos);
		int len = array.size();
		for (int i = 0; i < len; i++) {
			JSONObject list = (JSONObject) array.get(i);

			String typeName = list.getString(JSONConstants.ATTR_TYPEID);
			String tagName = list.getString(JSONConstants.ATTR_TAGNAME);

			if (typeName.equals(JSONConstants.ATTR_SUBWAY) && tagName.equals(line)) {
				ApartmentSearchChildDataInfo childDataInfo = new ApartmentSearchChildDataInfo(0,
						list.getString(JSONConstants.ATTR_DATA_NAME), true, dataInfo.getParentName(), dataInfo);
				childDataInfos.add(childDataInfo);
				childDataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
				childDataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
			}
		}
	}

	/**
	 * 递归移除Group<ApartmentSearchChildDataInfo>中没有子节点的非叶子节点
	 * 
	 * @param data
	 */
	public static void filterNullNoLeafNode(Group<ApartmentSearchChildDataInfo> data) {
		if (data == null || data.size() <= 0) {
			return;
		}

		for (int i = data.size() - 1; 0 <= i; i--) {
			ApartmentSearchChildDataInfo info = data.get(i);
			if (info.isLeaf()) {
				continue;
			} else {
				Group<ApartmentSearchChildDataInfo> childgroup = info.getChildDataInfos();
				if (childgroup == null || childgroup.size() <= 0) {
					data.remove(info);
				} else {
					filterNullNoLeafNode(childgroup);
				}
			}
		}
	}

	/**
	 * 将ApartmentSearchChildDataInfo对象data转换成ApartmentKeyword对象并返回
	 * 
	 * @param data
	 * @return
	 */
	public static ApartmentKeyword changeToApartmentKeyword(ApartmentSearchChildDataInfo data) {
		ApartmentKeyword keyword = new ApartmentKeyword();
		if (null != data) {
			int areaType = -1;
			String distinctId = "";
			double lan = 0;
			double lng = 0;
			if (data.getParentName().contains("区域")) {
				if ((data.getParentInfo()).getName().equals("商圈")) {
					areaType = ApartmentKeyword.TYPE_COMMERCIAL;
					distinctId = data.getId();
				} else if ((data.getParentInfo()).getName().equals("行政区")) {
					areaType = ApartmentKeyword.TYPE_DISTRICT;
					distinctId = data.getId();
				} 
//				else if ((data.getParentInfo()).getName().equals("机场/火车站")) {
//					areaType = ApartmentKeyword.TYPE_TRAFFIC;
//				} else if ((data.getParentInfo()).getName().equals("酒店品牌")) {
//					areaType = ApartmentKeyword.TYPE_BRAND;
//					distinctId = data.getId();
//				}else if((data.getParentInfo()).getName().equals("医院")){
//					areaType = ApartmentKeyword.TYPE_HOSPITAL;
//					distinctId = data.getId();
//				}else if((data.getParentInfo()).getName().equals("学校")){
//					areaType = ApartmentKeyword.TYPE_SCHOOL;
//					distinctId = data.getId();
//				}else { // 地铁
//					areaType = ApartmentKeyword.TYPE_SUBWAY_INFO;
//				}
			}

			if (StringUtils.isNotEmpty(data.getLatitude()) && StringUtils.isNotEmpty(data.getLongitude())) {
				lan = Double.parseDouble(data.getLatitude());
				lng = Double.parseDouble(data.getLongitude());
			}

			keyword.setName(data.getName());
			keyword.setParentName(data.getParentInfo().getName());
			keyword.setType(areaType);
			keyword.setLat(lan);
			keyword.setLng(lng);
			keyword.setId(distinctId);
		}
		return keyword;
	}

	/**
	 * 将关键字条件mKeyWordInfo转化为SelectSerchChildInfo
	 * 
	 * @param mKeyWordInfo
	 */
	public static ApartmentSelectSerchChildInfo changeToSelectSerchChildInfo(ApartmentKeyword mKeyWordInfo) {
		Group<ApartmentSearchChildDataInfo> childDataInfos = new Group<ApartmentSearchChildDataInfo>();
		ApartmentSelectSerchChildInfo mSSInfo = new ApartmentSelectSerchChildInfo(childDataInfos);
		if (!StringUtils.isEmpty(mKeyWordInfo.getName()) && mKeyWordInfo.getType() != ApartmentKeyword.TYPE_UNKNOWN
//				&& mKeyWordInfo.getType() != ApartmentKeyword.TYPE_HOTELNAME
//				&& mKeyWordInfo.getType() != ApartmentKeyword.TYPE_POI
				) {
			ApartmentSearchDataInfo parentInfo = new ApartmentSearchDataInfo();
			ApartmentSearchChildDataInfo info = new ApartmentSearchChildDataInfo();

			switch (mKeyWordInfo.getType()) {
			case ApartmentKeyword.TYPE_COMMERCIAL:
				parentInfo.setName("商圈");
				break;
			case ApartmentKeyword.TYPE_DISTRICT:
				parentInfo.setName("行政区");
				break;
//			case ApartmentKeyword.TYPE_SUBWAY_INFO:
//				parentInfo.setName(mKeyWordInfo.getParentName());
//				break;
//			case ApartmentKeyword.TYPE_TRAFFIC:
//				parentInfo.setName("机场/火车站");
//				break;
//			case ApartmentKeyword.TYPE_HOSPITAL:
//				parentInfo.setName("医院");
//				break;
//			case ApartmentKeyword.TYPE_SCHOOL:
//				parentInfo.setName("学校");
//				break;
			default:
				break;
			}
			info.setName(mKeyWordInfo.getName());
			info.setParentInfo(parentInfo);
			mSSInfo.getChildDataInfos().add(info);
		}
		return mSSInfo;
	}
	/**
	 * 发布房源选择区域位置中
	 * 将关键字条件mKeyWordInfo转化为SelectSerchChildInfo
	 *
	 * @param mKeyWordInfo
	 */
	public static ApartmentSelectSerchChildInfo changeToPublishHouseAreaSelectInfo(ApartmentKeyword mKeyWordInfo) {
		Group<ApartmentSearchChildDataInfo> childDataInfos = new Group<ApartmentSearchChildDataInfo>();
		ApartmentSelectSerchChildInfo mSSInfo = new ApartmentSelectSerchChildInfo(childDataInfos);
		if (!StringUtils.isEmpty(mKeyWordInfo.getName()) && mKeyWordInfo.getType() != ApartmentKeyword.TYPE_UNKNOWN
				) {
			ApartmentSearchDataInfo parentInfo = new ApartmentSearchDataInfo();
			ApartmentSearchChildDataInfo info = new ApartmentSearchChildDataInfo();

			switch (mKeyWordInfo.getType()) {
				case ApartmentKeyword.TYPE_COMMERCIAL:
					//商圈父类名称是行政区名称或者其他
					parentInfo.setName(mKeyWordInfo.getParentName());
					break;
				case ApartmentKeyword.TYPE_DISTRICT:
					//行政区父类名称是行政区名称本身
					parentInfo.setName(mKeyWordInfo.getName());
					break;

				default:
					break;
			}
			info.setName(mKeyWordInfo.getName());
			info.setParentInfo(parentInfo);
			mSSInfo.getChildDataInfos().add(info);
		}
		return mSSInfo;
	}
	/** 
	 * 发布房源选择区域位置中
	 * 将API返回的区域原始JSON数据(getLocationListV2接口)
	 * result转换成目前代码中需要的ApartmentSearchDataInfo,行政区包含商圈数据
	 * 
	 * @param result
	 * @return 
	 */
	public static ApartmentSearchDataInfo createPublishHouseAreaData(Object result) {
		ApartmentSearchDataInfo districtSerchDataInfo = null;
		try {
			if (result != null) {
				districtSerchDataInfo = new ApartmentSearchDataInfo();
				districtSerchDataInfo.setName("区域位置");
				districtSerchDataInfo.setSelect(true);
				districtSerchDataInfo.setIconResource(R.drawable.icon_district_selecter);
				Group<ApartmentSearchChildDataInfo> districtchildDataInfos = new Group<ApartmentSearchChildDataInfo>();
				districtSerchDataInfo.setChildDataInfos(districtchildDataInfos);
				
				//遍历数据取出行政区
				JSONObject obj = (JSONObject) result;
				JSONArray array = obj.getJSONArray(JSONConstants.ATTR_LOCATIONLIST);
				int len = array.size();
				for (int i = 0; i < len; i++) {
					JSONObject list = (JSONObject) array.get(i);
					String typeName = list.getString(JSONConstants.ATTR_TYPEID);
					if (TextUtils.isEmpty(typeName)) {
						continue;
					}
					if (typeName.equals(JSONConstants.ATTR_DISTRICT)){
						// 行政区
						ApartmentSearchChildDataInfo regionHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(
								R.drawable.icon_hotel_administrative_district, 
								list.getString(JSONConstants.ATTR_DATA_NAME), 
								false, districtSerchDataInfo.getName(),
								districtSerchDataInfo);
						regionHotelSerchChildDataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
						Group<ApartmentSearchChildDataInfo> regionChild = new Group<ApartmentSearchChildDataInfo>();
						regionHotelSerchChildDataInfo.setChildDataInfos(regionChild);
						districtchildDataInfos.add(regionHotelSerchChildDataInfo);
					}
				}
				ApartmentSearchChildDataInfo otherHotelSerchChildDataInfo = null;
				Group<ApartmentSearchChildDataInfo> otherChild = null;		
				//其他,id为null
				otherHotelSerchChildDataInfo = new ApartmentSearchChildDataInfo(R.drawable.icon_hotel_administrative_district,
						"其他",false, districtSerchDataInfo.getName(), districtSerchDataInfo);
				otherChild = new Group<ApartmentSearchChildDataInfo>();
				otherHotelSerchChildDataInfo.setChildDataInfos(otherChild);
				districtchildDataInfos.add(otherHotelSerchChildDataInfo);
				//遍历数据取出商圈
				for (int i = 0; i < len; i++) {
					JSONObject list = (JSONObject) array.get(i);
					String typeName = list.getString(JSONConstants.ATTR_TYPEID);
					if (TextUtils.isEmpty(typeName)) {
						continue;
					}
					if (typeName.equals(JSONConstants.ATTR_COMMERCIAL)){
						//商圈，父id与某行政区id相同，则将该商圈加入到行政区子类中
						String parentId = list.getString(JSONConstants.ATTR_PARENT_ID);
						boolean isBelong = false;
						for(ApartmentSearchChildDataInfo parentInfo:districtchildDataInfos){
							if(parentInfo.getId()!=null && parentInfo.getId().equals(parentId)){
								isBelong = true;
								ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo();
								dataInfo.setParentInfo(parentInfo);
								dataInfo.setLeaf(true);
								dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
								dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
								dataInfo.setParentName(parentInfo.getName());
								dataInfo.setParentId(parentId);//商圈记录父行政区id
								dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
								dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
								parentInfo.getChildDataInfos().add(dataInfo);
								continue;
							}
						}
						//id不是行政区的加入“其他”类别
						if(!isBelong){
							ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo();
							dataInfo.setParentInfo(otherHotelSerchChildDataInfo);
							dataInfo.setLeaf(true);
							dataInfo.setName(list.getString(JSONConstants.ATTR_DATA_NAME));
							dataInfo.setId(list.getString(JSONConstants.ATTR_DATA_ID));
							dataInfo.setParentName(otherHotelSerchChildDataInfo.getName());
							dataInfo.setLatitude(list.getString(JSONConstants.ATTR_LAT));
							dataInfo.setLongitude(list.getString(JSONConstants.ATTR_LNG));
							otherChild.add(dataInfo);
						}
					}
				}
				for(ApartmentSearchChildDataInfo parentInfo:districtchildDataInfos){
					//若某个行政区不包含商圈，则子类里添加行政区本身
					if(parentInfo.getId()!=null && parentInfo.getChildDataInfos().isEmpty()){
						ApartmentSearchChildDataInfo dataInfo = new ApartmentSearchChildDataInfo();
						dataInfo.setParentInfo(parentInfo);
						dataInfo.setLeaf(true);
						dataInfo.setName(parentInfo.getName());
						dataInfo.setId(parentInfo.getId());
						dataInfo.setParentName("行政区");
						dataInfo.setLatitude(parentInfo.getLatitude());
						dataInfo.setLongitude(parentInfo.getLongitude());
						parentInfo.getChildDataInfos().add(dataInfo);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return districtSerchDataInfo;
	}
	/**
	 * 发布房源选择区域位置中
	 * 将ApartmentSearchChildDataInfo对象data转换成ApartmentKeyword对象并返回
	 * 
	 * @param data
	 * @return
	 */
	public static ApartmentKeyword changeToPublishHouseAreaKeyword(ApartmentSearchChildDataInfo data) {
		ApartmentKeyword keyword = new ApartmentKeyword();
		if (null != data) {
			int areaType;
			double lan = 0;
			double lng = 0;
			if (data.getParentName().contains("行政区")) {
				areaType = ApartmentKeyword.TYPE_DISTRICT;
			}else{
				areaType = ApartmentKeyword.TYPE_COMMERCIAL;
			}

			if (StringUtils.isNotEmpty(data.getLatitude()) && StringUtils.isNotEmpty(data.getLongitude())) {
				lan = Double.parseDouble(data.getLatitude());
				lng = Double.parseDouble(data.getLongitude());
			}

			keyword.setName(data.getName());
			keyword.setParentName(data.getParentInfo().getName());
			keyword.setType(areaType);
			keyword.setLat(lan);
			keyword.setLng(lng);
			keyword.setId(data.getId());
			keyword.setParentId(data.getParentId());
		}
		return keyword;
	}
}
